package com.example.safeassistan.my.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.base.BaseModel;
import com.example.safeassistan.common.UserData;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.main.MainActivity;
import com.example.safeassistan.utile.JsonUtils;
import com.example.safeassistan.utile.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class ActivityModifyPassword extends BaseActivity implements View.OnClickListener {

    private EditText etnewPassword;
    private EditText etoldPassword;
    private TextView tvSave;
    private String oldpwd="";
    private String newpwd="";
    private String code="";
    private String phone="";
    private int type=1;


    public static void actionStart(Context context,String code,String phone) {
        Intent intent = new Intent(context, ActivityModifyPassword.class);
        intent.putExtra("code",code);
        intent.putExtra("phone",phone);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
          case  R.id.tvSave:
              submitPwd();
            break;
        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activitymodifypassword;
    }

    @Override
    protected void initView() {
        setTitleText("修改登录密码");
        code=getIntent().getStringExtra("code");
        phone=getIntent().getStringExtra("phone");
        tvSave=findViewById(R.id.tvSave);
        tvSave.setOnClickListener(this::onClick);
        etnewPassword=findViewById(R.id.etnewPassword);
        etoldPassword=findViewById(R.id.etConfirmPwd);
        etoldPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                try {
                    oldpwd = s.toString().trim();
                } catch (Exception e) {
                    oldpwd = "";
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        etnewPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                try {
                    newpwd = s.toString().trim();
                } catch (Exception e) {
                    newpwd = "";
                }
            }

            @Override
            public void afterTextChanged(Editable editable) { }
        });
    }

    private void submitPwd() {
        if(newpwd.equals("")||oldpwd.equals("")||newpwd.length()<=0||oldpwd.length()<=0){
            ToastUtil toastUtil=new ToastUtil(getContext(),R.layout.popu_success1,"请输入符合规定的密码",1);
            toastUtil.show();
            return;
        }
        if(!newpwd.equals(oldpwd)){
            ToastUtil toastUtil=new ToastUtil(getContext(),R.layout.popu_success1,"两次输入密码不一致",1);
            toastUtil.show();
            return;
        }
        Map map = new HashMap<>();
            map.put("passWord",newpwd);
        new HttpRequest(getContext()).doGet(UrlConstants.updatePayPassword, "", map,new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        ToastUtil toastUtil=new ToastUtil(getContext(),R.layout.popu_success1,"密码操作成功",1);
                        toastUtil.show();
                        MainActivity.actionStart(getContext(),4);
                        finish();
                    }else {
                        ToastUtil toastUtil=new ToastUtil(getContext(),R.layout.popu_success1,message,1);
                        toastUtil.show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                Log.e(TAG,msg);
            }
        });
    }

    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return 0;
    }



}
